<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item v-if="showBack" label="">
        <el-button @click="router.back()"
          ><Icon icon="ep:arrow-left" class="mr-5px" />返回</el-button
        >
      </el-form-item>
      <el-form-item label="交易单号" prop="tradeNo">
        <el-input
          v-model="queryParams.tradeNo"
          placeholder="请输入交易单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="支付单号" prop="payNo">
        <el-input
          v-model="queryParams.payNo"
          placeholder="请输入支付单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="已支付" prop="payed">
        <el-select
          v-model="queryParams.payed"
          placeholder="请选择支付状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="(dict, index) in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="用户信息" align="center" min-width="100px">
        <template #default="scope">
          <el-avatar :src="scope.row.user?.avatar" /><br />
          <el-tooltip :content="'用户编号：' + scope.row.userId" placement="top">
            <el-text>{{ scope.row.user?.nickname }}</el-text>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" min-width="80px">
        <template #default="scope">
          <el-tag type="primary">{{ scope.row.typeName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="有效天数" align="center" prop="days" />
      <el-table-column label="金额" align="center" prop="price">
        <template #default="scope">
          {{ fenToYuan(scope.row.price) }}
        </template>
      </el-table-column>
      <el-table-column label="单号" align="left" min-width="120">
        <template #default="scope">
          <div class="flex flex-col">
            <span>交易：{{ scope.row.tradeNo }}</span>
            <span>支付：{{ scope.row.payNo }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否已支付" align="center" prop="payed">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.payed" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" min-width="200" /> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { CloudAllianceApi } from '@/api/member/cloud-alliance/order'
import { fenToYuan } from '@/utils'

/** 套餐购买记录 列表 */
defineOptions({ name: 'CloudAllianceOrder' })

const route = useRoute() // 路由
const router = useRouter()

const loading = ref(true) // 列表的加载中
const list = ref<any>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  packageId: '',
  tradeNo: undefined,
  payNo: undefined,
  payed: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CloudAllianceApi.getPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 初始化 **/
const showBack = ref(false)
onMounted(() => {
  getList()
  if (route.query.packageId) {
    queryParams.packageId = route.query.packageId + ''
    showBack.value = true
  }
})
</script>
